package com.spring.sunflower.login;

import com.spring.sunflower.App;
import com.spring.sunflower.SplashActivity;
import com.spring.sunflower.base.BaseObserver;
import com.spring.sunflower.base.BasePresenter;
import com.spring.sunflower.base.BaseView;
import com.spring.sunflower.bean.AuthCodeBean;
import com.spring.sunflower.bean.LoginByAuthCodeBean;
import com.spring.sunflower.bean.VersionBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.util.LogUtils;
import com.spring.sunflower.util.SPUtils;

import io.reactivex.rxjava3.core.Observable;

/**
 * Created by Kevin on 2021/5/27<br/>
 * <p>
 * Describe:<br/>
 */
public class SplashPresenter extends BasePresenter<LoginContract.SplashView> {
    private String TAG = "LoginByAuthCodePresenter";
    private LoginContract.View view;

    public SplashPresenter(LoginContract.SplashView baseView) {
        super(baseView);
    }

    public void requestAuthCode(String phoneNum, String type) {
        Observable<AuthCodeBean> observable = requestDataSource.requestAuthCode(phoneNum, type);
        addDisposable(observable, new BaseObserver<AuthCodeBean>() {
            @Override
            public void onSuccess(AuthCodeBean bean) {
                baseView.hideLoading();
                baseView.requestFinish();
            }

            @Override
            public void onError(int status, String msg) {
                baseView.showError(status, msg);
            }
        });

    }

    public void loginByAuthCode(String phoneNum, String authCode) {
        Observable<LoginByAuthCodeBean> observable = requestDataSource.loginByAuthCode(phoneNum, authCode);
        addDisposable(observable, new BaseObserver<LoginByAuthCodeBean>(baseView) {
            @Override
            public void onSuccess(LoginByAuthCodeBean bean) {
                baseView.hideLoading();
                LoginByAuthCodeBean.DatasBean datas = bean.getDatas();
                String nickname = datas.getNickname();
                String nicknameWomen = datas.getNicknameWomen();
                saveField(Constant.KEY_REGISTER_NICKNAME, nickname);
                saveField(Constant.KEY_REGISTER_NICKNAME_FEMALE, nicknameWomen);
                String type = storageData(datas);
                baseView.requestFinishWithSth(type);
            }

            @Override
            public void onError(int status, String msg) {
                baseView.showError(msg);
            }
        });
    }

    public void loginByToken(String userID, String token) {
        Observable<LoginByAuthCodeBean> observable = requestDataSource.loginByToken(userID, token);
        addDisposable(observable, new BaseObserver<LoginByAuthCodeBean>(baseView) {
            @Override
            public void onSuccess(LoginByAuthCodeBean bean) {
                baseView.hideLoading();
                LoginByAuthCodeBean.DatasBean datas = bean.getDatas();
                String type = storageData(datas);
                baseView.requestFinishWithSth(type);
            }

            @Override
            public void onError(int status, String msg) {
                baseView.showError(status, msg);
            }
        });
    }

    public void loginByOneKey(String userID, String token) {
        Observable<LoginByAuthCodeBean> observable = requestDataSource.loginByOneKey(userID, token);
        addDisposable(observable, new BaseObserver<LoginByAuthCodeBean>(baseView) {
            @Override
            public void onSuccess(LoginByAuthCodeBean bean) {
                baseView.hideLoading();
                LoginByAuthCodeBean.DatasBean datas = bean.getDatas();
                String type = storageData(datas);
                baseView.requestFinishWithSth(type);
            }

            @Override
            public void onError(int status, String msg) {
                baseView.showError(status, msg);
            }
        });
    }

    public void loginByDevice() {
        Observable<LoginByAuthCodeBean> observable = requestDataSource.loginByDevice();
        addDisposable(observable, new BaseObserver<LoginByAuthCodeBean>(baseView) {
            @Override
            public void onSuccess(LoginByAuthCodeBean bean) {
                LoginByAuthCodeBean.DatasBean datas = bean.getDatas();
                String type = storageData(datas);
                String ticket = datas.getTicket();
                baseView.requestFinishWithSth(ticket);
            }

            @Override
            public void onError(int status, String msg) {
                baseView.showError(status, msg);
            }
        });
    }

    public void getVersion() {
        Observable<VersionBean> observable = requestDataSource.getVersion();
        addDisposable(observable, new BaseObserver<VersionBean>(baseView) {
            @Override
            public void onSuccess(VersionBean bean) {
                VersionBean.DatasBean datas = bean.getDatas();
                if (datas != null) {
                    baseView.getVersionSuccess(datas);
                    SPUtils.setSP("isVerify", App.getContext(),datas.getIsVerify());
                } else {
                    baseView.getVersionWithNull();
                }
            }

            @Override
            public void onError(int status, String msg) {
            }
        });
    }


    public String storageData(LoginByAuthCodeBean.DatasBean datas) {
        String token = datas.getToken();
        String sex = datas.getSex();
//                    Long birthday = datas.getBirthday();
        String sig = datas.getSig();
        LogUtils.INSTANCE.logD("MainActivity", "sig========" + sig);
        String nickname = datas.getNickname();
        String type = datas.getType();
        String id = datas.getId();
        String photo = datas.getPhoto();
        String isVip = datas.getIsVip();
        String mobile = datas.getMobile();
        String wealth = datas.getWealth();
        String todayRegister = datas.getTodayRegister();//是否是当天注册的新用户0否，1是

        saveField(Constant.KEY_TOKEN, token);
        saveField(Constant.KEY_GENDER, sex);
//        saveField("birthday", birthday);
        saveField(Constant.KEY_SIGNATURE, sig);
        saveField(Constant.KEY_NICKNAME, nickname);
        saveField(Constant.KEY_TYPE, type);
        saveField(Constant.KEY_USERID, id.toString());
        saveField(Constant.KEY_PHOTO, photo);
        saveField(Constant.KEY_MOBILE, mobile);
        saveField(Constant.KEY_WEALTH, wealth);
        saveField(Constant.KEY_NEWER, todayRegister);
        return type;
    }
}
